package school.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import school.domain.Friend;
import school.mapper.FriendMapper;
import school.support.BaseController;

import java.util.Date;

@RequestMapping("friend")
@Controller
public class FriendController extends BaseController {

    @Autowired
    private FriendMapper friendMapper;

    @RequestMapping("add/{id}")
    public String add(@PathVariable Integer id) {
        if (null == frontUser()) {
            session.setAttribute("errorMsg", "请先登录");
            return "redirect:/index";
        }
        Friend friend = new Friend();
        friend.setAddtime(new Date());
        friend.setUserid(frontUser().getId());
        friendMapper.insertSelective(friend);
        session.setAttribute("errorMsg", "关注成功");
        return "redirect:/member/index";
    }

    @RequestMapping("del/{id}")
    public String del(@PathVariable Integer id) {
        if (null == frontUser()) {
            session.setAttribute("errorMsg", "请先登录");
            return "redirect:/index";
        }
        Friend friend = new Friend();
        friend.setUserid(frontUser().getId());
        friend.setFriendid(id);
        friendMapper.delete(friend);
        return refresh();
    }

}
